using System;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class OldGenericRewardAnimator : MonoBehaviour
{
	[Serializable]
	public class EditorReferences
	{
		public ImageAnimationItem BaseItem;

		public float CircleSize = 100f;

		public TextAnimatedItem BaseText;

		public RectTransform DefaultLayer;

		public RectTransform DefaultOrigin;

		public RectTransform CashDefaultDestination;

		public RectTransform GemDefaultDestination;
	}

	[SerializeField]
	public EditorReferences references = new EditorReferences();

	private Queue<ImageAnimationItem> _readyItems = new Queue<ImageAnimationItem>();

	private List<ImageAnimationItem> _movingItems = new List<ImageAnimationItem>();

	private Queue<TextAnimatedItem> _readyTexts = new Queue<TextAnimatedItem>();

	private List<TextAnimatedItem> _movingTexts = new List<TextAnimatedItem>();

	private List<Sequence> _playingSequences = new List<Sequence>();

	private static OldGenericRewardAnimator _instance;

	public static OldGenericRewardAnimator Instance
	{
		get
		{
			if (!_instance)
			{
				_instance = UnityEngine.Object.FindObjectOfType<OldGenericRewardAnimator>();
			}
			return _instance;
		}
	}

	private void Awake()
	{
		if (_instance == null)
		{
			_instance = this;
		}
		else if (_instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		if (base.transform.parent == null)
		{
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		}
	}

	public void OnDisable()
	{
		Clear();
	}

	public void Clear()
	{
		foreach (Sequence playingSequence in _playingSequences)
		{
			if (playingSequence != null && playingSequence.IsPlaying())
			{
				playingSequence.Complete();
			}
		}
		_playingSequences.Clear();
		foreach (ImageAnimationItem movingItem in _movingItems)
		{
			movingItem.Tr.localScale = Vector3.one;
			movingItem.Img.sprite = null;
			movingItem.gameObject.SetActive(value: false);
			_readyItems.Enqueue(movingItem);
		}
		_movingItems.Clear();
		foreach (TextAnimatedItem movingText in _movingTexts)
		{
			_ = movingText.transform.localScale;
			movingText.transform.localScale = Vector3.one;
			movingText.gameObject.SetActive(value: false);
			_readyTexts.Enqueue(movingText);
		}
		_movingItems.Clear();
	}

	private void AnimateSingleCurrency(TextAnimatedItem text, List<ImageAnimationItem> currencyIcons, RectTransform destination, Action displayUpdateFunction, Action finalCallback, float delay = 0f)
	{
		Sequence newSequence = DOTween.Sequence();
		_playingSequences.Add(newSequence);
		if (text != null)
		{
			newSequence.InsertCallback(delay + 0.5f, delegate
			{
				text.Text.alpha = 1f;
				if (text != null)
				{
					text.gameObject.SetActive(value: true);
					text.transform.DOMoveY(text.transform.position.y + 200f, 1f);
					text.Text.DOFade(0f, 2f);
				}
			});
		}
		if (currencyIcons.Count == 0)
		{
			newSequence.AppendInterval(1f);
		}
		for (int i = 0; i < currencyIcons.Count; i++)
		{
			RectTransform iconsToMove = currencyIcons[i].Tr;
			iconsToMove.gameObject.SetActive(value: true);
			Vector2 vector = UnityEngine.Random.insideUnitCircle * references.CircleSize;
			newSequence.Insert(delay, iconsToMove.DOAnchorPos(iconsToMove.anchoredPosition + vector, 0.3f).SetEase(Ease.OutQuad));
			float num = 0f;
			for (int j = 0; j < i; j++)
			{
				num += Mathf.Max(0f, 0.1f - (float)j * 0.005f);
			}
			float num2 = Mathf.Max(0.1f, 0.7f - 0.02f * (float)i);
			newSequence.InsertCallback(delay + 0.3f + num + num2, delegate
			{
				if (displayUpdateFunction != null)
				{
					displayUpdateFunction();
				}
				if ((bool)CityBuildingManager.Instance)
				{
					CityBuildingManager.Instance.DoLightHaptic();
					CityBuildingManager.Instance.GameplayAudio.PlaySound(GameplaySoundType.Coin);
				}
			});
			newSequence.Insert(delay + 0.3f + num, iconsToMove.DOMove(destination.position, num2).SetEase(Ease.OutQuad).OnComplete(delegate
			{
				iconsToMove.gameObject.SetActive(value: false);
			}));
		}
		newSequence.AppendCallback(delegate
		{
			foreach (ImageAnimationItem currencyIcon in currencyIcons)
			{
				_readyItems.Enqueue(currencyIcon);
				_movingItems.Remove(currencyIcon);
				_playingSequences.Remove(newSequence);
				currencyIcon.Tr.SetParent(references.DefaultLayer, worldPositionStays: false);
				currencyIcon.gameObject.SetActive(value: false);
			}
			if (text != null)
			{
				_readyTexts.Enqueue(text);
				_movingTexts.Remove(text);
				text.Tr.SetParent(references.DefaultLayer, worldPositionStays: false);
				text.gameObject.SetActive(value: false);
			}
			if (finalCallback != null)
			{
				finalCallback();
			}
		});
	}

	public void AnimateCurrency(int qty, int steps, RectTransform origin, RectTransform destination, Vector2 size, bool useText, bool useCurrency, Action tickCallback, Action finalCallback, float delay, Transform parent, float circleSize = 200f, Sprite icon = null, bool useRandomRot = false)
	{
		if (qty == 0 || steps == 0)
		{
			finalCallback?.Invoke();
			return;
		}
		if (origin == null)
		{
			origin = references.DefaultOrigin;
		}
		if (destination == null)
		{
			destination = references.CashDefaultDestination;
		}
		if ((bool)CityBuildingManager.Instance)
		{
			CityBuildingManager.Instance.UIAudio.PlaySound(UISoundType.CoinAppear);
		}
		references.CircleSize = circleSize;
		List<ImageAnimationItem> list = new List<ImageAnimationItem>();
		if (useCurrency)
		{
			for (int i = 0; i < steps; i++)
			{
				ImageAnimationItem imageAnimationItem;
				if (_readyItems.Count != 0)
				{
					imageAnimationItem = _readyItems.Dequeue();
				}
				else
				{
					imageAnimationItem = UnityEngine.Object.Instantiate(references.BaseItem);
					imageAnimationItem.transform.localScale = Vector3.one;
				}
				list.Add(imageAnimationItem);
				_movingItems.Add(imageAnimationItem);
				(imageAnimationItem.Img.transform as RectTransform).sizeDelta = size;
				imageAnimationItem.Tr.SetParent(origin, worldPositionStays: false);
				imageAnimationItem.Tr.localPosition = Vector3.zero;
				imageAnimationItem.Tr.localScale = Vector3.one;
				if (parent == null)
				{
					imageAnimationItem.Tr.SetParent(base.transform, worldPositionStays: true);
				}
				else
				{
					imageAnimationItem.Tr.transform.SetParent(parent);
				}
				if (icon != null)
				{
					imageAnimationItem.Img.sprite = icon;
				}
				else
				{
					imageAnimationItem.Img.sprite = references.BaseItem.Img.sprite;
				}
				if (useRandomRot)
				{
					imageAnimationItem.Tr.localEulerAngles = new Vector3(0f, 0f, UnityEngine.Random.Range(0f, 360f));
				}
				else
				{
					imageAnimationItem.Tr.localEulerAngles = Vector3.zero;
				}
			}
		}
		TextAnimatedItem textAnimatedItem = null;
		if (useText)
		{
			textAnimatedItem = ((_readyTexts.Count == 0) ? UnityEngine.Object.Instantiate(references.BaseText) : _readyTexts.Dequeue());
			textAnimatedItem.Tr.SetParent(origin, worldPositionStays: false);
			textAnimatedItem.Tr.localScale = Vector3.one;
			textAnimatedItem.Tr.localPosition = Vector3.zero + Vector3.up * 100f;
			textAnimatedItem.Text.text = "+ " + qty;
			textAnimatedItem.gameObject.SetActive(value: false);
			_movingTexts.Add(textAnimatedItem);
			if (parent == null)
			{
				textAnimatedItem.Tr.SetParent(base.transform, worldPositionStays: true);
			}
			else
			{
				textAnimatedItem.Tr.transform.SetParent(parent);
			}
		}
		AnimateSingleCurrency(textAnimatedItem, list, destination, tickCallback, finalCallback, delay);
	}

	private Transform FindRootCanvasTransform(Transform t)
	{
		Transform result = t;
		Canvas componentInParent = t.GetComponentInParent<Canvas>();
		if (componentInParent != null)
		{
			componentInParent = componentInParent.rootCanvas;
			result = componentInParent.transform;
		}
		return result;
	}

	public void SetDefaultOriginPosition(Vector2 position)
	{
		references.DefaultOrigin.anchoredPosition = position;
	}
}
